package com.sp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sp.model.ConnDB;
import com.sp.model.NewsBean;

public class SelectNewsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SelectNewsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utff-8");
		response.setCharacterEncoding("utf-8");
		NewsBean newsSelective=(NewsBean) request.getAttribute("newsSelective");
		Connection connection =new ConnDB().getConn();
		ResultSet rs;
		NewsBean newsTemp;
		List<NewsBean> newsListSelective = new ArrayList<NewsBean>();
		try {
			Statement statement = connection.createStatement();
			StringBuffer sqlSentence = new StringBuffer();
			sqlSentence.append("SELECT * FROM userinfo where 1=1 ");
			if(newsSelective.getInhome()==1){
				sqlSentence.append(" and inhome=1 ");
			}
			else if(newsSelective.getInhome()==0){
				sqlSentence.append(" and inhome=0 ");
			}
			
			if(newsSelective.getIsvouch()==1){
				sqlSentence.append(" and isvouch=1 ");
			}
			else if(newsSelective.getIsvouch()==0){
				sqlSentence.append(" and isvouch=0 ");
			}
			
			if(newsSelective.getIstop()==1){
				sqlSentence.append(" and istop=1 ");
			}
			else if(newsSelective.getIstop()==0){
				sqlSentence.append(" and istop=0 ");
			}
			sqlSentence.append(" and tid="+newsSelective.getTid() +" ");
			if(newsSelective.getTitle()!=null){
				sqlSentence.append(" and ntitle LIKE '%"+newsSelective.getTitle()+"%' ");
			}
			rs = statement.executeQuery(sqlSentence.toString());
			while(rs.next()){
				newsTemp = new NewsBean();
				newsTemp.setId(rs.getInt(1));
				newsTemp.setTitle(rs.getString(2));
				newsTemp.setTid(rs.getInt(3));
				newsTemp.setUname(rs.getString(4));
				newsTemp.setContent(rs.getString(5));
				newsTemp.setPicture(rs.getString(6)!=null?rs.getString(6):"");
				newsTemp.setAddTime(rs.getDate(7));
				newsTemp.setUpdateTime(rs.getDate(8));
				newsTemp.setInhome(rs.getInt(9));
				newsTemp.setIsvouch(rs.getInt(10));
				newsTemp.setIstop(rs.getInt(11));
				newsListSelective.add(newsTemp);
			}
			if(connection!=null){
				connection.close();
			}
			if(statement!=null){
				statement.close();
			}
			if(rs!=null){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("newsList", newsListSelective);
		getServletConfig().getServletContext().getRequestDispatcher("newslist.jsp").forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
